Importing Python modules
Importing modules
CSense installs its own instance of Python 3.8.6. If you want to use Python modules in a library that is not part of the Python 3.8.6 installation (see the Supported Python libraries topic), you need to use the Python Module Manager application for installing Python libraries.
The CSense instance of Python can exist side-by-side with other instances of Python on your machine. When following the procedure below for importing Python modules, ensure that this procedure is performed using the Python Module Manager together with the CSense-managed instance of Python.
Notes:
Installing and running the Python Module Manager application may require internet access to download all required modules and their dependencies.
Running the Python Module Manager application requires Administrator privileges.
Perform the following steps to import Python modules:
-
Install the PIP tool (if not already installed):
-
From pypi.org, download Python PIP (in this instance version 20.2.4, but can be any version of your choice) at https://files.pythonhosted.org/packages/cb/28/91f26bd088ce8e22169032100d4260614fc3da435025ff389ef1d396a433/pip-20.2.4-py2.py3-none-any.whl to C:\Program Files (x86)\Proficy\Proficy CSense\Python38.
-
As Administrator, open a command prompt and navigate to C:\Program Files (x86)\Proficy\Proficy CSense.
-
Run PythonModuleManager.exe. This will launch a Graphical User Interface application.
-
Install PIP by clicking the "Add" button and selecting the file downloaded above in step 1. NOTE: The user can select an archive of previously-exported module - this would install all modules found in the exported archive file.
-
-
Install the third-party Python library with the Python Module Manager:
-
As an Administrator, open a command prompt and navigate to C:\Program Files (x86)\Proficy\Proficy CSense.
-
Run PythonModuleManager.exe.
-
Click the "Add" button and browse to the module that is to be installed. Yo u can select multiple modules placed in the same folder for installation.
-
Make a note of where your Python library is installed.
-
For example, the library may be installed to C:\Program Files (x86)\Proficy\Proficy CSense\Python38\lib\site-packages.
-
-
-
Add the folder location containing the newly installed Python library to the registry:
-
Open the Registry Editor.
-
Go to HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Proficy CSense.
-
Edit the PythonPath key, appending the Python library folder location to the list.
-
For example, you might append C:\Program Files (x86)\Proficy\Proficy CSense\Python38\lib\site-packages to the list. Items in the list are separated by semicolons.
-
-
-
Include a statement at the top of your Python script to import modules from the installed library.
-
For example, assuming that you have installed the path.py library to C:\Program Files (x86)\Proficy\Proficy CSense\Python\lib\site-packages, you can use the following example Python script. The script imports the path module from the Path library, and then uses it to find all the .txt files in a folder and iterate through the list:
-
Python: |
from path import Path if first_execute(): global textFileList global counter global d textFileList = [] d = Path(;C:\MyTextFileFolder;) for f in d.files(;*.txt;): textFileList.append(f.splitpath()[1]) counter = 0 textFileList = sorted(textFileList)
outputs.TextFileName.set(textFileList[counter]) counter=counter+1 |
Removing Modules
On the Module Manager list view, highlight the module to uninstall and click the "Remove" button. You can also select multiple modules to uninstall.
Alternate to the "Remove" button, the user can highlight (multi-select enabled) and right-click on modules to uninstall. From the context menu, select "Uninstall Modules"
Exporting Installed Modules
On the Module Manager list view, click the "Export" button. All currently-installed modules will be archived into a Zip file that gets stored in C:\Program Files (x86)\Proficy\Proficy CSense\Python Module Backup\PythonModules.pybackup.
Related topics: